// console.log(getApp().globalData)
var axios = require("../util/request.js")
var app = getApp()
// console.log(app)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dayStyle: [
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' },
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' }
    ],
    token:'',
    newday:'',
    startTime:'',
    endTime:'',
    type:'0',
    data:'',
    hiddenmodalput: false,
    time:'',
    amount:'',
    num:'',
    tempimg: '',
    tempimg1:false,
    base6:'',
    uptype:'',
    red:'',
    start:'',
    tabBar: '',
    id:''
  },
  //给点击的日期设置一个背景颜色
  bindDateChange1: function (e) {
    var data={
      "start_day": e.detail.value,
      "end_day": this.data.endTime,
    }
    if (e.detail.value < this.data.endTime){
      this.dateinterface(data)
    }else{
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
    }
   
    this.setData({
      startTime: e.detail.value,
      type:'1'
    })
  },
 
  bindDateChange2: function (e) {
   
    this.setData({
      endTime: e.detail.value,
      type: '2'
    })
    var data = {
      "end_day": e.detail.value,
      "start_day": this.data.startTime,
    }
    if (e.detail.value > this.data.startTime) {
      this.dateinterface(data)
    } else {
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
    }
    // this.dateinterface(data)
  },
  
//  请求日期接口
  dateinterface(data){
    axios.post('mall_manager/shop_input_show_data', data, this.shuffleSuc);
  },
  shuffleSuc(res){
    console.log(res)
  },
// 得到当前日期
 double(num){//让单数变成双数
    if(num<10) {
      return '0' + num;//单数前面添加0
    }else{
      return num;
    }
  },
		time() {
    var date = new Date();//创建一个日期对象
    var year = date.getFullYear()//获取4位年份
    var month = date.getMonth() + 1;//+1
    var day = date.getDate();
    var str = ''; 
    var hour = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
      str = year + '-' + this.double(month) + '-' + this.double(day)
      this.setData({
        newday:str
      })
   
      this.setData({
        endTime: str,
        startTime: str
      })
      var data = {
        "end_day": str,
        "start_day": str,
      }
      this.dateinterface(data)
      
      
      // 最大选择90天数据
      var date1 = new Date();
      var date2 = new Date(date1);
      date2.setDate(date1.getDate() - 90);
      var state = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate()
      this.setData({
        start: state
    
      })
      // console.log(state);
      
  },

  // 上传图片
  upimgs(){
    const that=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // console.log(that.urlTobase64(res.tempFilePaths))     
        // that.urlTobase64(res.tempFilePaths[0]) 
      

        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0], //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            // console.log('data:image/png;base64,' + res.data)
          var base64a = 'data:image/jpeg;base64,' + res.data
            that.setData({
              tempimg1: true,
              tempimg: base64a,
              base6: base64a
            })
          }
        })
       
       



        // tempFilePath可以作为img标签的src属性显示图片
        // const tempFilePaths = res.tempFilePaths
      }
    })
    
  },
// 图片转化
  urlTobase64(url1) {
    const that=this
    wx.request({
      url: url1,
      responseType: 'arraybuffer', //最关键的参数，设置返回的数据格式为arraybuffer
      success: res => {
        //把arraybuffer转成base64
        let base64 = wx.arrayBufferToBase64(res.data);

        //不加上这串字符，在页面无法显示的哦
        base64 　= 'data:image/jpeg;base64,' + base64
        that.setData({
          base6: base64
        })
        //打印出base64字符串，可复制到网页校验一下是否是你选择的原图片呢
        console.log(base64)
      }
    })
  },
  // 取消
  cancelM: function (e) {
    this.setData({
      hiddenmodalput: false,
    })
  },
  // 提交
  confirmM: function (e) {
    // console.log(this.data.uptype)
    // console.log(this.data.amount <= 10000000)
    if (this.data.amount == '' || this.data.amount == null || this.data.amount==undefined){
      wx.showToast({
        title: '金额不能为空',
        icon:'none'
      })
    } else if (this.data.num == '' || this.data.num == null || this.data.num==undefined){
      wx.showToast({
        title: '笔数不能为空',
        icon: 'none'
      })
    } else if (this.data.amount >=10000000 ){
      wx.showToast({
        title: '金额过大',
        icon: 'none'
      })
    } else if (this.data.num >= 10000000) {
      wx.showToast({
        title: '笔数过大',
        icon: 'none'
      })
    }
    else{
      var json = {
        'day': this.data.time,
        'amount': Number(this.data.amount),
        'num': this.data.num,
        'photo': this.data.base6
      }
      if (this.data.uptype == '1') {
        axios.post('mall_manager/shop_input_data', JSON.stringify(json), this.shuffleSuc1);
      } else {
        axios.post('mall_manager/shop_update_data', JSON.stringify(json), this.shuffleSuc2);
      }
      this.setData({
        hiddenmodalput: false,
      })
    }  
    // console.log("姓名：" + this.data.name + "  电话：" + this.data.phoneNum);
  },
  // 请求上报成功
  shuffleSuc1(res){
     
    var data = {
      "end_day": this.data.endTime,
      "start_day": this.data.startTime,
    }
    this.dateinterface(data)
  },
  // 下拉刷新
  onPullDownRefresh: function(a){
    
    var data = {
      "end_day": this.data.endTime,
      "start_day": this.data.startTime,
    }
    this.dateinterface(data)
    wx.stopPullDownRefresh()
  },
  
  // 请求修改成功
  shuffleSuc2(res){   
    var data = {
      "end_day": this.data.endTime,
      "start_day": this.data.startTime,
    }
    this.dateinterface(data)
  },
  amount(a){
    this.setData({
      amount: a.detail.value
    })
    
  },
  num(a) {
    this.setData({
      num: a.detail.value
    })
 
  },
  // 修改
  addrevamp(value){
 
    this.setData({
      uptype:'2',
      time: value.currentTarget.dataset.id.day,
      amount: value.currentTarget.dataset.id.upload_amount ,
      num: value.currentTarget.dataset.id.upload_number,
      hiddenmodalput: true,
      amount: '',
      num: '',
      base6:'',
      tempimg:'',
      tempimg1:false
    })
  },
  // 上报
  updata(value) {
  
    this.setData({
      uptype:'1',
      time: value.currentTarget.dataset.id.day,
      hiddenmodalput:true,
      amount: '',
      num: '',
      base6: '',
      tempimg1:false,
      tempimg:''
    })
    
  },
  onLoad: function (id) { 
    
    this.time()
    app.editTabBar()
    
    this.setData({
      tabBar:app,
      id:id.id
    })
    
    // wx.switchTab({
    //   url: '/pages/data/data'
    // })
    // wx.setTabBarItem({
    //   index: 0,
    //   pagePath: "pages/data/data",
    //   text: 'text',
    //   iconPath: '/pages/data/data',
    //   selectedIconPath: '/path/to/selectedIconPath'
    // })
   },
  shuffleSuc(res){
    if (res.data.code =='E000000'){
      //  console.log(res.data.data.items)
      const data_items = res.data.data.items
      // data_items.forEach((item,index,value)=>{
      //   console.log(item)
      //   if ((item.upload_status)=='' || (item.upload_status)=='unll' || (item.upload_status)=='undefind'){
      //     this.setData({
      //       red: red
      //     })
      //   } else if (item.upload_status == '1'){
      //     this.setData({
      //       red: '#999'
      //     })
      //   } else if (item.upload_status == '2'){
      //     this.setData({
      //       red: '#333'
      //     })
      //   } else if (item.upload_status == '3') {
      //     this.setData({
      //       red: '#409eff'
      //     })
      //   }
      // })
        this.setData({
          data: res.data.data.items.reverse()
        })
    }else{
      wx.showLoading({
        title: '暂无数据',
        mask: true
      })
      setTimeout(()=>{
        wx.hideLoading()
      },500)
    }
    // console.log(res)
  }
})
